
var config = require('config.js');


function apiData(option) {

	var _option = {
		url: config.API_URL,
		data: {},
		header: {
			'Content-Type': 'application/json'
		},
		success: function (res) { },
		fail: function (res) {
			wx.showToast({
				title: '请求失败',
				duration: 1000
			});
		},
		complete: function (res) {

		}

	}


	option = extend(_option, option);
	option.url = config.API_URL + option.url;


	wx.showToast({
		title: '加载中...',
		icon: 'loading',
		duration: 5000,
		mask: true
	});


	wx.request(option);
}

function extend() {
	var arr = arguments,
		result = {},
		i;

	if (!arr.length) return {};

	var _isObject = function (o) {
		return Object.prototype.toString.call(o) === '[object Object]';
	}

	var _extend = function self(destination, source) {
		var property;
		for (property in destination) {
			if (destination.hasOwnProperty(property)) {

		
				if (_isObject(destination[property]) && _isObject(source[property])) {
					self(destination[property], source[property]);
				};

				if (source.hasOwnProperty(property)) {
					continue;
				} else {
					source[property] = destination[property];
				}
			}
		}
	}

	for (i = arr.length - 1; i >= 0; i--) {
		if (_isObject(arr[i])) {
			_extend(arr[i], result);
		};
	}

	arr[0] = result;
	return result;
}


function trim(_str) {
	return _str.replace(/(^\s*)|(\s*$)/g, "");
}


module.exports = {
	extend: extend,
	apiData: apiData,
	trim: trim
};